package com.jianjunhuang.demo.wechatdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class HomepageListViewAdapter extends BaseAdapter {

    private List<WeChatBean> mList;
    private LayoutInflater inflater;

    public HomepageListViewAdapter(Context context, List<WeChatBean> mList) {
        this.mList = mList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.wechat_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.avatarImg = convertView.findViewById(R.id.wechat_list_item_avatar_img);
            viewHolder.nickNameTv = convertView.findViewById(R.id.wechat_list_item_name_tv);
            viewHolder.contentTv = convertView.findViewById(R.id.wechat_list_item_content_tv);
            viewHolder.timeTv = convertView.findViewById(R.id.wechat_list_item_time_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        WeChatBean bean = mList.get(position);
        viewHolder.avatarImg.setImageResource(bean.getAvatar());
        viewHolder.nickNameTv.setText(bean.getNickName());
        viewHolder.timeTv.setText(bean.getTime());
        viewHolder.contentTv.setText(bean.getContent());

        return convertView;
    }

    class ViewHolder {
        ImageView avatarImg;
        TextView nickNameTv;
        TextView contentTv;
        TextView timeTv;
    }
}
